package models.report;

import helpers.ClientRequest;

import java.util.*;

import play.Logger;
import play.db.ebean.*;
import play.db.ebean.Model.Finder;
import play.data.validation.Constraints.*;
import play.data.format.*;
import play.mvc.Http.Context;
import play.mvc.Http.Request;
import service.Paging;

import javax.persistence.*;

import com.avaje.ebean.Ebean;
import com.avaje.ebean.EbeanServer;
import com.avaje.ebean.ExpressionList;
import com.avaje.ebean.Page;
import com.avaje.ebean.PagingList;
import com.avaje.ebean.Query;
import com.avaje.ebean.RawSql;
import com.avaje.ebean.RawSqlBuilder;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;

@Entity
public class User extends Model{
	@JsonIgnore
	public Integer dateKey;
	
	@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd")
	public Date dateValue;
	
	@JsonIgnore
	public Integer appId;
	
	@JsonIgnore
	public Integer marketKey;
	
	@JsonIgnore
	public Integer deviceKey;
	
	// Extra
	public int registerNums;
	public int invitedRegisterNums;
	public int tutorialCompletedNums;
	
	public int dau;
	public int vipNums;
	public int newVipNums;
	public int repeaterVipNums;
	
	public static Finder<Long,User> find = new Finder(
		Long.class, User.class
	);
	
	public static Paging search(String type, int startTime, int endTime){		
		Request request = Context.current().request();
		
		int pageSize = Integer.parseInt(request.getQueryString("pageSize"));
    	int page = Integer.parseInt(request.getQueryString("page"));
        
    	EbeanServer report = Ebean.getServer("report");
    	
    	String sql = "select date_value, register_nums, invited_register_nums, "
			+ " tutorial_completed_nums, dau, vip_nums, new_vip_nums, repeater_vip_nums" 
    		+ " from rpt_user_daily";  
      
    	if(ClientRequest.getCurrent().marketKey == 0)
    	{
    		sql = " select date_value, SUM(register_nums) as registerNums, SUM(invited_register_nums) as invitedRegisterNums, "
				+ " SUM(tutorial_completed_nums) as tutorialCompletedNums , "
				+ " SUM(dau) as dau, SUM(vip_nums) as vipNums, SUM(new_vip_nums) as newVipNums, SUM(repeater_vip_nums) as repeaterVipNums "  
    			+ " from rpt_user_daily "   
    			+ " group by date_value";  
    	}
    	
    	RawSql rawSql = RawSqlBuilder.parse(sql).create();
            
    	Query<User> query = report.find(User.class);
	    query.setRawSql(rawSql);
	    
    	ExpressionList<User> expression = query.where();
    	
    	if(startTime > 0) expression.ge("date_key", startTime);
    	
    	if(endTime > 0) expression.le("date_key", endTime);
    	
    	expression = helpers.Query.addClientRequestExpression(expression);
    	
    	PagingList<User> pagingList = expression.findPagingList(pageSize).setFetchAhead(false);
    	
    	Page<User> page0 = pagingList.getPage(page - 1);
        List<User> list = page0.getList();
        
    	Paging paging = new Paging(expression.findList().size(), list);
    	
        return paging;
	}
}
